package dao;

import bean.User;

import java.sql.*;

public class UserDao {
    public UserDao() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 链接数据库
    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/coursejg?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root",
                "1234");
    }


    // 判断用户名是否存在
    public boolean queryUser(User user) {

        String username = user.getUsername();
        try (Connection c = getConnection(); Statement s = c.createStatement();) {
            // 创建sql语句
            String sql = "select * from user where username=?";
            // 加载sql语句
            PreparedStatement preparedStatement = c.prepareStatement(sql);
            // 设置sql中的参数
            preparedStatement.setString(1, username);
            //执行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //循环取值
            while (resultSet.next()) {
                String id1 = resultSet.getString("id");
                String username1 = resultSet.getString("username");
                String password = resultSet.getString("password");
                String name = resultSet.getString("name");
                if (username.equals(username1)) return true;
            }


        } catch (SQLException e) {

            e.printStackTrace();
        }
        return false;
    }

    // 判断密码是否正确
    public User queryPassword(User user) {

        String username = user.getUsername();
        String password = user.getPassword();
        try (Connection c = getConnection(); Statement s = c.createStatement();) {
            // 创建sql语句
            String sql = "select * from user where username=?";
            // 加载sql语句
            PreparedStatement preparedStatement = c.prepareStatement(sql);
            // 设置sql中的参数
            preparedStatement.setString(1, username);
            //执行sql语句
            ResultSet resultSet = preparedStatement.executeQuery();
            //循环取值
            while (resultSet.next()) {
                int id1 = resultSet.getInt("id");
                String username1 = resultSet.getString("username");
                String password1 = resultSet.getString("password");
                String name1 = resultSet.getString("name");

                // 如果登录成功
                if (username.equals(username1) && password.equals(password1)) {
                    // 将数据库中的查询数据保存到对象中
                    User user1 = new User();
                    user1.setName(name1);
                    user1.setPassword(password1);
                    user1.setUsername(username1);
                    user1.setId(id1);
                    return user1;
                }

            }


        } catch (SQLException e) {

            e.printStackTrace();
        }
        return null;
    }

    public boolean registerUser(User user) {

        String query = "INSERT INTO user (username, password, name) VALUES (?, ?, ?)";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getName());

            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}


